package com.ltu.controller.customize;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ltu.model.request.base.BaseFilterPageReq;
import com.ltu.model.request.base.BaseIdReq;
import com.ltu.model.response.base.CodeDataResp;
import com.ltu.service.customize.PushAdService;
import com.ltu.service.impl.BaseServiceImpl;
import com.tencentcloudapi.cms.v20190321.models.Device;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.ltu.ai.sparkdesk.websocket.AiContextManager;
import com.ltu.domain.mp_entity.customize.DeviceEntity;
import com.ltu.domain.mp_entity.customize.PushAdEntity;
import com.ltu.mapper.customize.DeviceMapper;


/**
 * <p>
 * 设备 前端控制器
 * </p>
 *
 * @author 若尘
 * @since 2024-09-26
 */
@RestController

@RequestMapping("/Device")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags= "设备")
public class DeviceController extends BaseServiceImpl<DeviceMapper, DeviceEntity>{
	private final AiContextManager  aiContextManager;
	private final PushAdService  pushAdService;
	
	
    @ApiOperation(value="获取分页列表")
    @RequestMapping(value="/getMePage", method= RequestMethod.POST)
    public CodeDataResp<Page<DeviceEntity>> getMePage(@RequestBody  BaseFilterPageReq<DeviceEntity> req ) {
    	return super.getMePage(req);
    }
    
    @ApiOperation(value="获取列表")
    @RequestMapping(value="/getList", method= RequestMethod.POST)
    public CodeDataResp<List<DeviceEntity>> getList(@RequestBody  BaseFilterPageReq<DeviceEntity> req ) {
        return super.getMeList(req);
    }

    @ApiOperation(value="新增/保存")
    @RequestMapping(value="/edit", method= RequestMethod.POST)
    public CodeDataResp edit(@RequestBody DeviceEntity req) {
        return super.edit(req);
    }
    
//    @ApiOperation(value="批量修改数据")
//    @RequestMapping(value="/edits", method= RequestMethod.POST)
    public CodeDataResp edits(@RequestBody List<DeviceEntity> reqs) {
    	super.saveOrUpdateBatch(reqs);
    	return   CodeDataResp.valueOfSuccessEmptyData();
    }
    
    
    	@ApiOperation(value="获取详情")
    @RequestMapping(value="/getDetail/{id}", method= RequestMethod.GET)
    public CodeDataResp<DeviceEntity> getDetail(@PathVariable String id) {
 		BaseIdReq req=new BaseIdReq(id);
        return super.getDetail(req);
    }

    	@ApiOperation(value="切换新会话")
    	@RequestMapping(value="/cutSession", method= RequestMethod.GET)
    	public CodeDataResp<DeviceEntity> cutSession(@RequestParam String deviceMac) {
    		if(!AiContextManager.IatContextMap.containsKey(deviceMac))
				return CodeDataResp.valueOfFailed("设备不存在");
    		aiContextManager.cutSession(deviceMac);
    		return CodeDataResp.valueOfSuccessEmptyData();
    	}
    	

 	 @ApiOperation(value="删除")
     @RequestMapping(value="/remove/{id}", method= RequestMethod.GET)
     public CodeDataResp remove(@PathVariable String id) {
 		 DeviceEntity device =  this.getById(id);
       	QueryWrapper<PushAdEntity>   qw = new QueryWrapper<PushAdEntity>();
     	qw.lambda().eq(PushAdEntity :: getDeviceId, id);
     	List<PushAdEntity>  list = pushAdService.list(qw);
     	 super.removeById(id);
     	if(!CollectionUtils.isEmpty(list)) {
     		List<Integer> ids = list.stream().map(a -> Integer.parseInt(a.getId())).distinct().collect(Collectors.toList());
     		pushAdService.removeByIds(ids );
     		List<String> deviceMacs = Arrays.asList(device.getDeviceNo());
     		aiContextManager.pushAd(deviceMacs);
     	}
     	 
		return CodeDataResp.valueOfSuccessEmptyData();
     }
     
 //   @ApiOperation(value="删除s")
//    @RequestMapping(value="/removes", method= RequestMethod.POST)
    public CodeDataResp removes(@RequestBody List<Integer> ids) {
    		super.removeByIds(ids);
        return CodeDataResp.valueOfSuccessEmptyData();
    }

}

